package waggle.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.jets3t.service.security.AWSCredentials;

import com.xerox.amazonws.sdb.SimpleDB;
import com.xerox.amazonws.sqs2.QueueService;

/**
 * Utilities used by all Sample code, collected in one place for convenience.
 *
 * @author James Murty
 */
public class SamplesUtils {

    public static final String SAMPLES_PROPERTIES_NAME = "aws.properties";
    public static final String AWS_ACCESS_KEY_PROPERTY_NAME = "aws.accessId";
    public static final String AWS_SECRET_KEY_PROPERTY_NAME = "aws.secretKey";

    /**
     * Loads AWS Credentials from the file <tt>samples.properties</tt>
     * ({@link #SAMPLES_PROPERTIES_NAME}) that must be available in the
     * classpath, and must contain settings <tt>awsAccessKey</tt> and
     * <tt>awsSecretKey</tt>.
     *
     * @return
     * the AWS credentials loaded from the samples properties file.
     */
    public static SimpleDB loadASWDB() throws IOException {

        InputStream propertiesIS =
            ClassLoader.getSystemResourceAsStream( SAMPLES_PROPERTIES_NAME);

        if (propertiesIS == null) {
            throw new RuntimeException("Unable to load test properties file from classpath: "
                + SAMPLES_PROPERTIES_NAME);
        }

        Properties testProperties = new Properties();
        testProperties.load(propertiesIS);

        if (!testProperties.containsKey(AWS_ACCESS_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_ACCESS_KEY_PROPERTY_NAME);
        }
        if (!testProperties.containsKey(AWS_SECRET_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_SECRET_KEY_PROPERTY_NAME);
        }
    	SimpleDB sds = new SimpleDB(testProperties.getProperty(AWS_ACCESS_KEY_PROPERTY_NAME), testProperties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME), false);
    	
    	return sds;
    }
    
    
    public static QueueService loadASWQUEUE() throws IOException {

        InputStream propertiesIS =
            ClassLoader.getSystemResourceAsStream( SAMPLES_PROPERTIES_NAME);

        if (propertiesIS == null) {
            throw new RuntimeException("Unable to load test properties file from classpath: "
                + SAMPLES_PROPERTIES_NAME);
        }

        Properties testProperties = new Properties();
        testProperties.load(propertiesIS);

        if (!testProperties.containsKey(AWS_ACCESS_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_ACCESS_KEY_PROPERTY_NAME);
        }
        if (!testProperties.containsKey(AWS_SECRET_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_SECRET_KEY_PROPERTY_NAME);
        }
    	QueueService qs = new QueueService(testProperties.getProperty(AWS_ACCESS_KEY_PROPERTY_NAME), testProperties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME) );
    	return qs;
    }
    
    
    
    public static AWSCredentials loadAWSCredentials() throws IOException {
        InputStream propertiesIS =
            ClassLoader.getSystemResourceAsStream( SAMPLES_PROPERTIES_NAME);

        if (propertiesIS == null) {
            throw new RuntimeException("Unable to load test properties file from classpath: "
                + SAMPLES_PROPERTIES_NAME);
        }

        Properties testProperties = new Properties();
        testProperties.load(propertiesIS);

        if (!testProperties.containsKey(AWS_ACCESS_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_ACCESS_KEY_PROPERTY_NAME);
        }
        if (!testProperties.containsKey(AWS_SECRET_KEY_PROPERTY_NAME)) {
            throw new RuntimeException(
                "Properties file 'test.properties' does not contain required property: " + AWS_SECRET_KEY_PROPERTY_NAME);
        }

        AWSCredentials awsCredentials = new AWSCredentials(
            testProperties.getProperty(AWS_ACCESS_KEY_PROPERTY_NAME),
            testProperties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME));

        return awsCredentials;
    }

}
